package 回溯;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName _46全排列
 * @Description TODO
 * @Author 昝亚杰
 * @Date 2021/11/22 20:22
 * Version 1.0
 **/
public class _46全排列 {
    List<Integer> result = new ArrayList<>();
    List<List<Integer>> results = new ArrayList<>();
    int[] used;
    public List<List<Integer>> permute(int[] nums) {
        used = new int[nums.length];
        backTrack(nums);
        return results;
    }
    public void backTrack(int[] nums){
        if(result.size() == nums.length){
            results.add(new ArrayList<>(result));
        }
        for(int i = 0; i < nums.length; i++){
            if(used[i] == 1){
                continue;
            }
            result.add(nums[i]);
            used[i] = 1;
            backTrack(nums);
            result.remove(result.size() -1);
            used[i] = 0;
        }
    }
}
